<?php
	/**
	 * 
	 */
	class UserAction extends CommonAction {
		
		function _filter(&$map){
			$map['id'] = array('egt',2);
			$map['account'] = array('like',"%".$_POST['account']."%");
		}
		
		// 检查帐号
		public function checkAccount() {
	        if(!preg_match('/^[a-z]\w{4,}$/i',$_POST['account'])) {
	            $this->error( '用户名必须是字母，且5位以上！');
	        }
			$User = M("User");
	        // 检测用户名是否冲突
	        $name  =  $_REQUEST['account'];
	        $result  =  $User->getByAccount($name);
	        if($result) {
	        	$this->error('该用户名已经存在！');
	        }else {
	           	$this->success('该用户名可以使用！');
	        }
	    }
		
		public function update()
		{
			$user_model = D("User");
			//$role_model = D("role_user");
			
			$_POST['account'] = trim($_POST['account']);
			$_POST['nickname'] = trim($_POST['nickname']);
			$_POST['password'] = trim($_POST['password']);
			$_POST['repassword'] = trim($_POST['repassword']);
			
			if (!empty($_POST['password'])){
				if ($_POST['password'] != $_POST['repassword']){
					$this->error ('密码不匹配!');
				}else{
					$_POST['password'] = md5($_POST['password']);
				}
			}else{
				unset( $_POST['password'] );
				unset( $_POST['repassword'] );
			}
			
			if ($user_model->where("id='".$_POST['user_id']."'")->save($_POST)){
				//$role_model->where("user_id='".$_POST['user_id']."'")->setField('role_id',$_POST['role']);
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('编辑成功!');
			}else{
				$this->error ('编辑失败!');
			}
		}

		public function delete()
		{
			$user_model = D('User');
			$role_model = D('RoleUser');
			if (!empty($_POST['userid'])){
				foreach($_POST['userid'] as $key=>$val){
					$user_model->where("id='".$val."'")->delete();
					$role_model->where("user_id='".$val."'")->delete();
				}
				@header("location:".$_SERVER['HTTP_REFERER']);
				exit();
			}
		}
		/*
		public function _before_add()
		{
			$role_model = M('role');
			$role_data = $role_model->where("status='1'")->select();
			$this->assign("list",$role_data);
		}
		*/
		/*public function _before_edit()
		{
			$user_model = M('role_user');
			$user_data = $user_model->where("user_id='".$_GET['id']."'")->find();
			$this->assign("currrole",$user_data['role_id']);
			//$this->_before_add();
		}*/
		
		public function insert()
		{
			$model = D('User');
			if (!empty($_POST['password'])){
				$_POST['password'] = md5($_POST['password']);
			}
			if ( $model->add($_POST) ){
				$this->addRole($result,$_POST['role']);
				$this->ajaxReturn(1,'添加成功',1);
			}else{
				$this->ajaxReturn(0,'添加失败',0);
			}
		}
		
		/*protected function addRole($userID,$roleID)
		{
			$roleUser = D('role_user');
			$roleUser->user_id = $userID;
			$roleUser->role_id = $roleID;
			$roleUser->add();
		}*/
		protected function addRole($userId) {
			//新增用户自动加入相应权限组
			$RoleUser = M("RoleUser");
			$RoleUser->user_id	=	$userId;
	        // 默认加入网站编辑组
	        $RoleUser->role_id	=	18;
			$RoleUser->add();
		}
		
		//重置密码
	    public function resetPwd()
	    {
	    	$id  =  $_POST['id'];
	        $password = $_POST['password'];
	        if(''== trim($password)) {
	        	$this->error('密码不能为空！');
	        }
	        $User = M('User');
			$User->password	=	md5($password);
			$User->id			=	$id;
			$result	=	$User->save();
	        if(false !== $result) {
	            $this->success("密码修改为$password");
	        }else {
	        	$this->error('重置密码失败！');
	        }
	    }
		
	}
	
?>